package com.rc.evcharger.mq;

import com.alibaba.fastjson.JSON;
import com.rc.evcharger.vo.charge.StartChargeReq;
import com.rc.evcharger.service.CommandService;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Create by Yang on 2019/11/15
 *
 * 3秒延迟消费
 */
@Component("commandCardNoListener")
public class CommandCardNoListener implements ChannelAwareMessageListener {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private CommandService commandService;

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        String msg = new String(message.getBody(), "UTF-8");
        logger.info("cardNoStartChargeListener 收到了:{},channel:{}", msg, channel.getConnection());

        StartChargeReq startChargeRequest;
        try {
            startChargeRequest = JSON.parseObject(msg, StartChargeReq.class);
            startChargeRequest.setStartType("1");//启动方式 0 一般 1 刷卡 2 VIN码启动 默认0
            commandService.query_start_charge(startChargeRequest);
        } catch (Exception e) {
            logger.warn("cardNoStartChargeListener 处理失败", e);
            return;
        }
    }
}
